<template>
  <a-modal :open="open" width="920px" :title="rowData?.id ? '修改物品' : '新增物品'" centered ok-text="保存" @ok="onOk" @cancel="onCancel">
    <a-tabs v-model:activeKey="tabActiveKey">
      <a-tab-pane key="基本信息" tab="基本信息" />
      <a-tab-pane key="上海医保" tab="上海医保" />
    </a-tabs>
    <a-form
      v-if="tabActiveKey == '基本信息'"
      :model="formState"
      class="form-style-flex form-style-disabled h-[510px] content-start form-style-disabled"
      :label-col="{ span: 9 }"
    >
      <a-form-item label="" class="w-2/6">
        <a-radio-group v-model:value="formState.xmlb" @change="formState.materialType = []">
          <a-radio :value="30">卫生材料</a-radio>
          <a-radio :value="31">易耗品</a-radio>
          <a-radio :value="32">固定资产</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="物资属性" class="w-4/6">
        <a-checkbox-group v-model:value="formState.materialType">
          <a-checkbox :disabled="formState.xmlb != 30" value="1">高值耗材</a-checkbox>
          <a-checkbox :disabled="formState.xmlb != 31" value="2">防疫物资</a-checkbox>
          <a-checkbox :disabled="formState.xmlb != 32" value="3">卫生装备</a-checkbox>
        </a-checkbox-group>
      </a-form-item>

      <a-form-item label="物品名称" class="w-2/6">
        <a-input v-model:value="formState.mc" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="注册证号" class="w-2/6">
        <a-input v-model:value="formState.zczh" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="助记码" class="w-2/6">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="第二助记码" class="w-2/6">
        <a-input v-model:value="formState.zjm2" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="规格" class="w-2/6">
        <a-input v-model:value="formState.gg" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="物价编码" class="w-2/6">
        <a-input v-model:value="formState.wjbm" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="采购单位" class="w-44" :label-col="{ span: 12 }">
        <a-input v-model:value="formState.dw" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="使用单位" class="w-44" :label-col="{ span: 12 }">
        <a-input v-model:value="formState.zxdw" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="采购单位/使用单位" class="w-56 ml-1" :label-col="{ span: 15 }">
        <a-input-number v-model:value="formState.zsb" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="有效时间" class="w-4/12">
        <a-input v-model:value="formState.qgpNumber" placeholder="请输入">
          <template #addonAfter>
            <a-select v-model:value="formState.qgpUnit" style="width: 72px">
              <a-select-option :value="0">年</a-select-option>
              <a-select-option :value="1">月</a-select-option>
              <a-select-option :value="2">天</a-select-option>
              <a-select-option :value="3">小时</a-select-option>
            </a-select>
          </template>
        </a-input>
      </a-form-item>

      <a-form-item label="零售价格" class="w-2/6">
        <a-input-number v-model:value="formState.dj" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="安全库存量" class="w-2/6">
        <a-input v-model:value="formState.aqkc" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="最佳库存量" class="w-2/6">
        <a-input v-model:value="formState.zjkc" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="单位采购量" class="w-2/6">
        <a-input v-model:value="formState.dwcgl" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="厂家" class="w-4/6" :label-col="{ span: 5 }">
        <a-input v-model:value="formState.cj" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="产地" class="w-full" :label-col="{ span: 3 }">
        <a-input v-model:value="formState.cd" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="病案类别" class="w-1/2" :label-col="{ span: 6 }">
        <a-input v-model:value="formState.mrCategoryName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="会员类别" class="w-1/2">
        <SelectMember v-model:value="formState.memberCategoryCode" />
      </a-form-item>
      <a-form-item label="使用指导" class="w-full" :label-col="{ span: 3 }">
        <a-textarea v-model:value="formState.syzd" :rows="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
    <a-form v-if="tabActiveKey === '上海医保'" :model="formStateSH" class="form-style-flex h-[510px] content-start" :label-col="{ span: 9 }">
      <a-form-item label="省医保码" class="w-1/2">
        <a-input v-model:value="formStateSH.lmiCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="物价编码" class="w-1/2">
        <a-input v-model:value="formStateSH.phCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="分类自付比例" class="w-1/2">
        <a-input v-model:value="formStateSH.rbmRatio" placeholder="请输入" suffix="%" />
      </a-form-item>
      <a-form-item label="医保限额" class="w-1/2">
        <div class="flex items-center">
          <a-switch v-model:checked="formStateSH.miLimitFlag" class="mr-4" :un-checked-value="0" :checked-value="1" />
          <a-input v-model:value="formStateSH.miLimit" placeholder="请输入" />
        </div>
      </a-form-item>
      <a-form-item label="英文名称" class="w-1/2">
        <a-input v-model:value="formStateSH.engName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="项目细分" class="w-1/2">
        <a-switch v-model:checked="formStateSH.subFlag" :un-checked-value="0" :checked-value="1" />
      </a-form-item>
      <a-form-item label="注册证号" class="w-1/2">
        <a-input v-model:value="formStateSH.certificateSn" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  xmlb: 30,
  materialType: []
})
const formStateSH = ref({})
const tabActiveKey = ref('上海医保')

watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.bh) {
      formStateSH.value = { ...props.rowData }
      formState.value = { ...props.rowData, materialType: props.rowData.materialType ? [props.rowData.materialType] : [] }
      formState.value.xmlb = Number(props.rowData.xmlb)
    } else {
      formStateSH.value = {}
      formState.value = {
        xmlb: 30,
        materialType: []
      }
    }
  }
)

const onCancel = () => {
  formState.value = {
    xmlb: 30,
    materialType: []
  }
  formStateSH.value = {}
  emit('update:open', false)
}
const onOk = async () => {
  const { code, msg } = await post('operation/opex/shanghai/doSaveShmiDictMtr', { ...props.rowData, ...formStateSH.value })
  if (code !== 0) return message.error(msg)
  message.success('保存成功')
  emit('success')
  onCancel()
}
</script>
